<template>
  <el-upload
    class="avatar-uploader"
    action=""
    :show-file-list="false"
    :before-upload="beforeAvatarUpload"
    :http-request="uploadImage"
  >
    <div v-if="value" class="imgBox">
      <img v-if="value" :src="value" class="avatar">
      <span class="changeImg">Change</span>
    </div>
    <i v-else class="el-icon-plus avatar-uploader-icon" />
    <span slot="tip" class="el-upload-tip myTip">
      支持&nbsp;
      <span v-for="(item, index) in type" :key="index">
        {{ `.${item.split('/')[1]}格式、 ` }}
      </span>
    </span>
  </el-upload>
</template>

<script>
import COS from 'cos-js-sdk-v5'

export default {
  name: 'ImageUpload',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      type: ['image/jpeg', 'image/png', 'image/gif', 'image/bmp']
    }
  },
  methods: {
    // 检查文件类型&大小
    beforeAvatarUpload(file) {
      const isType = this.type.includes(file.type)
      const isLt2M = file.size / 1024 / 1024 < 5

      if (!isType) {
        this.$message.error('上传文件不符合格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 5MB!')
      }
      return isType && isLt2M
    },
    // 选择图片上传[腾讯云]
    uploadImage(params) {
      // COS对象初始化
      const cos = new COS({
        SecretId: 'AKIDviHbgW8VdWAZqsfDWinuyypX1ISjvsfG', // 应用标识
        SecretKey: 'XWyQn6JPK7mGcqvnbDcomX5OTKxTowre' // 应用密钥
      })
      cos.putObject({
        Bucket: 'godboy-1313463748', // 存储桶名称
        Region: 'ap-guangzhou', // 地域名称
        Key: params.file.name, // 文件名称
        StorageClass: 'STANDARD',
        Body: params.file // 文件对象
      }, (err, data) => {
        if (data.statusCode === 200 && data.Location) {
          this.$emit('input', `http://${data.Location}`)
        } else {
          this.$message.error(err)
        }
      })
    }
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: all .2s linear;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;

    div.imgBox > span.changeImg {
      display: block;
    }
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    object-fit: cover;
  }
  span.myTip {
    position: absolute;
    text-align: left;
    top: 0;
    bottom: 0;
    padding-left: 1em;
    color: #888;
    text-wrap: wrap;
    width: 30em;
    max-height: 178px;
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-line-clamp: 4;
  }
  div.imgBox {
    display: flex;
    position: relative;
    justify-content: center;
    align-items: center;

    span.changeImg {
      position: absolute;
      color: #333;
      top: 50%;
      width: 50%;
      height: 30px;
      line-height: 30px;
      border-radius: 5vw;
      background: rgba(255, 255, 255, .1);
      backdrop-filter: blur(3px);
      transition: all .2s ease-in-out;
      display: none;

      &:hover {
        background: rgba(0, 0, 0, .1);
        backdrop-filter: blur(2px);
        color: #e3e3e3;
      }
    }
  }
</style>
